package com.gitee.wsl.ext.list


/**
 * The same as [zipWithNext], but includes link between last and first element
 */
inline fun <T, R> List<T>.zipWithNextCircular(transform: (a: T, b: T) -> R): List<R> {
    if (size < 2) return emptyList()
    return indices.map { i ->
        if (i == size - 1) {
            transform(last(), first())
        } else {
            transform(get(i), get(i + 1))
        }
    }
}

fun <T> List<T>.zipWithNextCircular(): List<Pair<T, T>> = zipWithNextCircular { l, r -> l to r }

inline fun <T, U> List<T>.zipMinSize(other: List<T>, block: (T, T) -> U) =
    List(kotlin.math.min(size, other.size)) { block(this[it], other[it]) }